﻿//Wrapper Object to keep things out of global scope
var SpacePort = {};
//Array to hold Ships
SpacePort.ships = [];
//Spaceship Constructor
SpacePort.Ship = function (name, affiliation, image) {
    this.name = name;
    this.affiliation = affiliation;
    this.image = image;
};
//Set Prototype
SpacePort.Ship.prototype.isImpounded = false;
//Draw table to the page
SpacePort.drawTable = function () {
    var h = "<div class='container'>";
    h += "<div class='row'>";
    for (var i in SpacePort.ships) {
        var display = "bg-info";
        var currentShip = SpacePort.ships[i];
        if (currentShip.isImpounded) {
            display = "bg-danger";
        }
        h += "<div class='col-md-4 "+display+"'>";
        h += "<h1>";
        h += "<i class='" + SpacePort.getIconName(currentShip.affiliation) + "'></i> ";
        h += currentShip.name + "</h1><br/>";
        h += "<img src='" + currentShip.image + "' class='img-responsive img-thumbnail ship-image'/><br/>";
        if (currentShip.isImpounded) {
            h += "<div class='btn btn-danger fa fa-lock disabled'></div>";
        }
        else {
            h += "<div class='btn btn-danger fa fa-rocket' onclick='SpacePort.scrapShip(" + i + ")'></div>";
        }
        h += "<div class='btn btn-warning fa fa-edit' onclick='SpacePort.showEdit(" + i + ")'></div>"
        h += "</div>";
    }
    h += "</div>";
    h += "</div>";
    document.getElementById("tableHolder").innerHTML = h;
};
//Helper Function to pull Icon Names
SpacePort.getIconName = function (affiliation) {
    if (affiliation === "Imperial") {
        return "fa fa-empire";
    }
    else if (affiliation === "Rebel") {
        return "fa fa-rebel";
    }
    else {
        return "fa fa-rocket";
    }

};
/***************** CRUD *******************************/
//Add Ships to array
SpacePort.addShip = function () {
    "use strict";
    var name, affiliation, image;
    name = document.getElementById("name");
    affiliation = document.getElementById("affiliation");
    image = document.getElementById("image");
    var s = new SpacePort.Ship(name.value, affiliation.value, image.value);
    SpacePort.ships.push(s);
    image.value = "";
    name.value = "";
    SpacePort.drawTable();
};
//Delete Ship
SpacePort.scrapShip = function (index) {
    SpacePort.ships.splice(index, 1);
    SpacePort.drawTable();
};
//Show Edit Ship
SpacePort.showEdit = function (index) {
    var ship = SpacePort.ships[index];
    document.getElementById("editTitle").innerHTML = "Ship information for the " + ship.name;
    document.getElementById("editName").value = ship.name;
    document.getElementById("editImage").value = ship.image;
    document.getElementById("editAffiliation").value = ship.affiliation;
    document.getElementById("editImpound").value = ship.isImpounded.toString();
    document.getElementById("saveChangesBtn").onclick = function () { SpacePort.saveEdit(index); };
    $("#editModal").modal();

};
//Save Edit Ship
SpacePort.saveEdit = function (index) {
    var ship = SpacePort.ships[index];
    ship.name = document.getElementById("editName").value;
    ship.image = document.getElementById("editImage").value;
    ship.affiliation = document.getElementById("editAffiliation").value;
    if (document.getElementById("editImpound").value ==="true") {
        ship.isImpounded = true;
    }
    else {
        delete ship.isImpounded;
    }
    //SpacePort.ships[index].name = document.getElementById("editName").value;
    $("#editModal").modal("hide");
    SpacePort.drawTable();
};
/******************End CRUD****************************/
/*****************AJAX*********************************/
//Send to Firebase
SpacePort.sendShip = function (ship) {
    //Instantiate a new XMLHttp request object
    var request = new XMLHttpRequest();
    //Puts Postage and Address on the envelope
    request.open("POST", "https://testalex2.firebaseio.com/.json", true);
    //Defines what happens when the response loads
    request.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            //Got a valid response code
            alert(this.response);
        }
        else {
            console.log("Error "+this.status + ":" + this.response);
        }
    };
    //What happens if the request response load never occurs
    request.onerror = function () {
        console.log("Communication error");
    };
    //Sends the request
    request.send(JSON.stringify(ship));

};
//Pull from Firebase
SpacePort.getShips = function () {
    //Instantiate a new XMLHttp request object
    var request = new XMLHttpRequest();
    //Puts Postage and Address on the envelope
    request.open("GET", "https://testalex2.firebaseio.com/.json", true);
    //Defines what happens when the response loads
    request.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            //Got a valid response code
            alert(this.response);
            //Make it an object
            var data = JSON.parse(this.response);
            //Break it apart into individual ships
            for (var x in data) {
                //Figure out what x is...we'll need it later
                data[x].__proto__ = SpacePort.Ship.prototype;
                //Add ship to the array
                SpacePort.ships.push(data[x]);
            }
            SpacePort.drawTable();
        }
        else {
            console.log("Error " + this.status + ":" + this.response);
        }
    };
    //What happens if the request response load never occurs
    request.onerror = function () {
        console.log("Communication error");
    };
    //Sends the request
    request.send();
};
//NEED UPDATE AND DELETE

/******************End AJAX***************************/
//Seed Function
SpacePort.seed = function () {
    SpacePort.ships.push(new SpacePort.Ship("Millenium Falcon", "Other", "https://www.crestws.com/wp-content/uploads/Lego-Millenium-Falcon.jpg"));
    SpacePort.ships.push(new SpacePort.Ship("Shuttle", "Imperial", "http://cache.lego.com/e/dynamic/is/image/LEGO/10212?$main$"));
    SpacePort.ships.push(new SpacePort.Ship("Slave-1", "Other", "http://img1.wikia.nocookie.net/__cb20100822062932/lego/images/5/5b/Slave_1-2.png"));
    SpacePort.ships.push(new SpacePort.Ship("X-Wing", "Rebel", "/content/xwing.png"));
    SpacePort.drawTable();
};
//Clear All
SpacePort.clearAll = function () {
    SpacePort.ships = [];
    SpacePort.drawTable();
};
//Run Seed Function
SpacePort.seed();
